<?php
/*
Galaxy
Author:McFog
*/
defined('IN GALAXY') or exit('McFog');

class model_seccode {
	var $resdir = RESDIR;
	var $width = 80, $height = 40;
	var $im, $fontcolor;
	function output() {
		$this->im = imagecreatefromstring($this->background());
		$str = $this->getcode();
		imagettftext($this->im, mt_rand(20,28),
		  mt_rand(-20,5),
		  10, 30,
		  $this->randcolor(),
		  $this->randfont(),
		  substr($str,0,3));
		imagettftext($this->im, mt_rand(20,28),
		  mt_rand(-5,20),
		  40, 30,
		  $this->randcolor(),
		  $this->randfont(),
		  substr($str,3,3));
		$this->adulterate();
		header("Content-type: image/png");
		imagepng($this->im);
		imagedestroy($this->im);
	}

	function background() {
		$this->im = imagecreatetruecolor($this->width, $this->height);
		$backgroundcolor = imagecolorallocate($this->im, 255, 255, 255);
		$backgrounds = $c = array();
		if(function_exists('imagecreatefromjpeg') && function_exists('imagecolorat') &&	function_exists('imagecopymerge') &&
			function_exists('imagesetpixel') && function_exists('imageSX') && function_exists('imageSY')) {
			if($handle = @opendir($this->resdir.'secbg/')) {
				while($bgfile = @readdir($handle)) {
					if(preg_match('/\.jpg$/i', $bgfile)) {
						$backgrounds[] = $this->resdir.'secbg/'.$bgfile;
					}
				}
				@closedir($handle);
			}
			if($backgrounds) {
				$imwm = imagecreatefromjpeg($backgrounds[array_rand($backgrounds)]);
				$colorindex = imagecolorat($imwm, 0, 0);
				$c = imagecolorsforindex($imwm, $colorindex);
				$colorindex = imagecolorat($imwm, 1, 0);
				imagesetpixel($imwm, 0, 0, $colorindex);
				$c[0] = $c['red'];$c[1] = $c['green'];$c[2] = $c['blue'];
				imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 200 - $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($imwm), 100);
				imagedestroy($imwm);
			}
		} else {
			for($i = 0;$i < 3;$i++) {
				$start[$i] = mt_rand(200, 255);$end[$i] = mt_rand(100, 150);$step[$i] = ($end[$i] - $start[$i]) / $this->width;$c[$i] = $start[$i];
			}
			for($i = 0;$i < $this->width;$i++) {
				$color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]);
				imageline($this->im, $i, 0, $i-$angle, $this->height, $color);
				$c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2];
			}
			$c[0] -= 20;$c[1] -= 20;$c[2] -= 20;
		}
		ob_start();
		if(function_exists('imagepng')) {
			imagepng($this->im);
		} else {
			imagejpeg($this->im, '', 100);
		}
		imagedestroy($this->im);
		$bgcontent = ob_get_contents();
		ob_end_clean();
		$this->fontcolor = $c;
		return $bgcontent;
	}
	
	function randfont() {
		$fonts = array();
		if($handle = @opendir($this->resdir.'font/')) {
			while($file = @readdir($handle)) {
				if(preg_match('/\.ttf$/i', $file)) {
					$fonts[] = $this->resdir.'font/'.$file;
				}
			}
			@closedir($handle);
		}
		$fonts or galaxy::halt("Failed to find font to generate seccode");
		return $fonts[array_rand($fonts)];
	}
	
	function adulterate() {
		$linenums = $this->height / 10;
		for($i=0; $i <= $linenums; $i++) {
			//$color = $this->color ? imagecolorallocate($this->im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
			$color = $this->randcolor();
			$x = mt_rand(0, $this->width);
			$y = mt_rand(0, $this->height);
			if(mt_rand(0, 1)) {
				imagearc($this->im, $x, $y, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, 360), mt_rand(0, 360), $color);
			} else {
				imageline($this->im, $x, $y, $linex + mt_rand(0, $linemaxlong), $liney + mt_rand(0, mt_rand($this->height, $this->width)), $color);
			}
		}
	}

	function randcolor($im = '') {
		$im!=='' or $im = $this->im;
		mt_srand();
		$arr = array($this->fontcolor[0],$this->fontcolor[1],$this->fontcolor[2]);
		$t = mt_rand(0,2);
		$arr[$t] += mt_rand(16, 48);
		$arr[3-$t] -= mt_rand(16, 48);
		foreach($arr as $k => $v) if($v>255) $arr[$k] = 255;
		return imagecolorallocate($im, $arr[0], $arr[1], $arr[2]);
	}
	
	
	function getcode() {
		$wordlist = include(RESDIR.'wordlist.php');
		return $wordlist[array_rand($wordlist)];
	}
}

?>